import React, { Component } from 'react';
import 'antd/dist/antd.css';
import store from './store'
import { getInitList, getInputChangeAction, getBtnClickAction, getDeleteItemAction } from './store/actionCreators'
import TodoListUI from './TodoListUi'
import axios from 'axios'
// 设置默认的请求域名
axios.defaults.baseURL = 'http://127.0.0.1:3001/'

class TodoList extends Component {
  constructor(props) {
    super(props);
    console.log(store)
    // 获取仓库数据
    this.state = store.getState()
    this.InputChange = this.InputChange.bind(this)
    this.BtnClick = this.BtnClick.bind(this)
    this.DeleteItem = this.DeleteItem.bind(this)
    this.StateChange = this.StateChange.bind(this)
    // 仓库数据改变触发 函数
    store.subscribe(this.StateChange)
  }
  render() {
    return (<TodoListUI
      inputValue={this.state.inputValue}
      InputChange={this.InputChange}
      BtnClick={this.BtnClick}
      list={this.state.list}
      DeleteItem={this.DeleteItem}
    />)
  }
  componentDidMount() {
    // getTodoList() 返回的是一个函数 指向了 action 
    // const action = getTodoList()
    // console.log('action函数', action)
    // 执行该函数 dispatch 中的如果是对象就是任务分发 
    // 如果是函数 就会自动执行该函数
    // store.dispatch(action)
    // axios.get('api/todolist').then((res) => {
    //   console.log(res)
    //   const action = InitStateAction(res.data)
    //   store.dispatch(action)
    // }).catch((err) => {
    //   console.log(err)
    // })

    // 使用saga 
    const action = getInitList()
    console.log(action)
    store.dispatch(action)
  }
  InputChange(e) {
    // const action = {
    //   type: 'CHANGE_INPUT_VALUE',
    //   value:e.target.value
    // }
    const action = getInputChangeAction(e.target.value)
    store.dispatch(action)
  }
  StateChange() {
    console.log('仓库内容改变')
    this.setState(store.getState())
  }
  BtnClick() {
    const action = getBtnClickAction()
    store.dispatch(action)
  }
  DeleteItem(index) {
    console.log('传递的索引')
    console.log(index)
    const action = getDeleteItemAction(index)
    store.dispatch(action)

  }
}
export default TodoList